<template>
    <div class="mv">
        <video ref="videoDOM" src="" controls autoplay></video>
        <div class="songName">{{ route.query.artistName }}</div>
        <div class="author">
            <img v-lazys="route.query.cover" alt="">
            <div>{{ route.query.name }}</div>
            <button>+ 关注</button>
        </div>
        <div class="Mvinfo">
            <div>发布时间：2013-10-08</div>
            <div>播放：{{ formatNumber(Number(route.query.playCount)) }}</div>
        </div>
        <!-- 评论组件 -->
        <Comment :comment="comment" :vid="String(route.query.id)" @postAComments="postAComments" />
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import * as mvAPI from '@/api/mv'
import useCounterStore from '@/stores/counter'
import Comment from '@/components/Comment.vue'
const { formatNumber } = useCounterStore()
const route = useRoute()
const videoDOM: any = ref(null)
const comment: any = ref({})

onMounted(() => {
    // 获取视频url和评论
    Promise.all([mvAPI.getMvUrl({ id: route.query.id }), mvAPI.getComments({ id: Number(route.query.id) })]).then(([res1, res2]) => {
        // 则将返回的url赋值给videoDOM
        videoDOM.value.src = res1.data.data.url
        // 将返回的评论赋值给comment
        comment.value = res2.data
    })
})

function postAComments(val: any) {
    // 调用子组件的方法，将评论内容传递给子组件
    comment.value.comments.unshift(JSON.parse(val));
}

</script>

<style lang="scss" scoped>
.mv::-webkit-scrollbar {
    display: none;
}

.mv {
    width: 100%;
    height: 89%;
    overflow: auto;
    margin-top: 10vh;
}

video {
    width: 90%;
    height: 60vh;
    object-fit: fill;
    margin-left: 4vw;
    border-radius: 10px;
}

.songName {
    color: #fff;
    margin: 1vw 4vw;
}

.author {
    margin-left: 4vw;
    /* margin-top: 4vw; */
    display: flex;
    align-items: center;
    color: rgba(255, 255, 255, 0.5);

    img {
        width: 4vw;
        height: 4vw;
        border-radius: 10px;
        margin-right: 14px;
    }

    >div {
        font-size: 12px;
    }

    button {
        border: 1px solid rgba(254, 0, 0, 0.5);
        border-radius: 4px;
        color: rgba(255, 0, 0, 0.8);
        font-size: 10px;
        padding: 4px 10px;
        margin-left: 20px;
        font-weight: 800;
        cursor: pointer;
        background: rgba(255, 255, 255, 0);
    }
}

.Mvinfo {
    width: 90%;
    display: flex;
    color: rgba(255, 255, 255, 0.5);
    margin-left: 4vw;
    margin-top: 10px;
    font-size: 12px;

    div {
        margin-right: 3vw;
    }
}
</style>